package view

import (
	"fmt"
	"github.com/wailsapp/wails/v2/pkg/runtime"
	"miao-wails/backed/app/service"
	"miao-wails/backed/lib/utils"
	"miao-wails/backed/structs/base"
	"miao-wails/backed/structs/tables"
)

func (a *App) TarFile(fileStr string) (err error) {
	return service.TarSer.TarFile(fileStr)
}

func (a *App) GetFileList(in base.SearchTarFile) (li []tables.TarFile, err error) {
	return service.TarSer.GetFileList(in)
}

func (a *App) DelFile(fid string) (err error) {
	return service.TarSer.DelFile(utils.StrToInt64(fid))
}

func (a *App) UpdateFileInfo(in base.UpdateTarFileInfo) (err error) {
	return service.TarSer.UpdateFileInfo(in)
}
func (a *App) ChangeFilePrivate(fid string) (err error) {
	return service.TarSer.ChangeFilePrivate(utils.StrToInt64(fid))
}
func (a *App) OpenTarFileDialog() (string, error) {
	options := runtime.OpenDialogOptions{
		Title:            "选择归档文件", // 对话框标题
		DefaultDirectory: "",       // 默认路径（空表示当前目录）
	}
	filePath, err := runtime.OpenFileDialog(a.ctx, options)
	if err != nil {
		return "", fmt.Errorf("对话框打开失败: %v", err)
	}
	if filePath == "" {
		return "", fmt.Errorf("用户取消了选择")
	}
	return filePath, nil
}

/////////////////////////////////////////

func (a *App) ChangePrivate(idStr string) (err error) {
	return service.TarSer.ChangePrivate(idStr)
}

func (a *App) DeleteMusic(idStr string) (err error) {
	return service.TarSer.DeleteMusic(idStr)
}

func (a *App) ChangeLoop() (str string, err error) {
	return service.TarSer.ChangeLoop()
}

func (a *App) GetSongInfo(whereStr string) (m tables.TarMusic, err error) {
	return service.TarSer.GetSongInfo(whereStr)
}

func (a *App) GetMusicList(showPrivate bool) (li []tables.TarMusic, err error) {
	return service.TarSer.GetMusicList(showPrivate)
}

func (a *App) SelectMusicFiles() (err error) {
	var (
		files []string
	)
	files, err = runtime.OpenMultipleFilesDialog(a.ctx, runtime.OpenDialogOptions{
		Title:            "选择多个文件",
		DefaultDirectory: "", // 默认路径（空表示当前目录）
		Filters: []runtime.FileFilter{
			{
				DisplayName: "支持音频格式：mp3、wav、wma、flac",
				Pattern:     base.MusicFileSuffix,
			},
		},
	})
	if err != nil {
		return
	}
	if len(files) == 0 {
		return
	}
	for i := range files {
		err = service.TarSer.MusicTar(files[i])
		if err != nil {
			return
		}
	}
	return nil
}
